package com.example.shishaolong.warehousemanager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.shishaolong.warehousemanager.entity.AdminEntity;

import java.util.List;

/**
 * 管理员列表适配器 适配 ListView
 * @author 时少龙
 */
public class AdminAdapter extends BaseAdapter {

    private List<AdminEntity> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public AdminAdapter(Context context, List<AdminEntity> list){
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //从ArrayList 中取出一个AdminEntity
        AdminEntity adminEntity = (AdminEntity) getItem(position);

        //获取显示数据对象的布局文件
        View view;
        if(convertView == null){
            view =  mInflater.inflate(R.layout.admin_item,null);
        }else {
            view = convertView;
        }

        //获取布局上的View对象，
        CheckBox checkBox = (CheckBox)view.findViewById(R.id.admin_item_checkBox);
        TextView name = (TextView) view.findViewById(R.id.admin_item_name);
        TextView level = (TextView)view.findViewById(R.id.admin_item_lever_value);
        TextView addDate = (TextView)view.findViewById(R.id.admin_item_add_date_value);

        //将实体类中的数据填写到各自的TextView中
        checkBox.setChecked(adminEntity.isChecked());
        name.setText(adminEntity.getName());
        level.setText((adminEntity.getLevel()==1)?"超级管理员":"普通管理员");
        addDate.setText(adminEntity.getAddDate());

        return view;
    }
}
